/*
 * Copyright (c) 2024. hallele, Inc All rights reserved.
 */

package cn.hallele.ms.portalcenter.application.das;

import cn.hallele.infrastructure.metadata.io.Page;
import cn.hallele.infrastructure.util.ObjectUtil;
import cn.hallele.mc.sdk.usercenter.common.enums.DeptStatusEnum;
import cn.hallele.ms.infrastructure.core.convertor.das.ModelDas;
import cn.hallele.ms.portalcenter.application.bo.DeptBO;
import cn.hallele.ms.portalcenter.application.bo.DeptQueryBO;
import cn.hallele.ms.portalcenter.domain.action.dept.DeptDomainService;
import cn.hallele.ms.portalcenter.domain.domain.DeptDomain;
import cn.hallele.ms.portalcenter.domain.domain.DeptQueryDomain;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * hallele_ms_portalcenter
 *
 * @author anle5
 * @since 2024/11/16 18:54
 */
@Slf4j
@Service
public class DeptBDDas extends ModelDas<DeptDomainService, DeptBO, DeptDomain> {
    protected DeptBDDas(DeptDomainService option) {
        super(option);
    }

    /**
     * 分页查询部门信息
     *
     * @param condition 查询条件
     * @return 部门信息
     */
    public Page<DeptBO> findDeptList(DeptQueryBO condition) {
        return revertPage(this.option.findDeptList(convertQuery(condition)));
    }

    /**
     * 查询单个部门信息
     *
     * @param condition 查询条件
     * @return 部门信息
     */
    public DeptBO findDept(DeptQueryBO condition) {
        return revert(this.option.findDept(convertQuery(condition)));
    }

    /**
     * 新增 部门
     *
     * @param deptBO 部门
     */
    public void addDept(DeptBO deptBO) {
        this.option.addDept(convert(deptBO));
    }

    /**
     * 更新 部门
     *
     * @param deptBO 部门
     */
    public void updateDept(DeptBO deptBO) {
        this.option.updateDept(convert(deptBO));
    }

    /**
     * 删除 部门
     *
     * @param id 要删除的dept id
     */
    public void deleteDept(Integer id) {
        this.option.deleteDept(id);
    }

    /**
     * 更新 部门 状态
     *
     * @param id     主键
     * @param status 状态
     */
    public void updateDeptStatus(Integer id, DeptStatusEnum status) {
        this.option.updateDeptStatus(id, status);
    }

    /**
     * 将T转成E
     *
     * @param deptQueryBO 被转换者
     * @return S 转换后者
     */
    private DeptQueryDomain convertQuery(DeptQueryBO deptQueryBO) {
        return ObjectUtil.map(deptQueryBO, d -> DeptQueryDomain.builder()
                .keywords(d.getKeywords())
                .startTime(d.getStartTime())
                .endTime(d.getEndTime())
                .pageNo(d.getPageNo())
                .pageSize(d.getPageSize())
                .id(d.getId())
                .name(d.getName())
                .phone(d.getPhone())
                .status(d.getStatus())
                .build());
    }

    /**
     * 将T转成E
     *
     * @param deptBO 被转换者
     * @return S 转换后者
     */
    @Override
    protected DeptDomain convert(DeptBO deptBO) {
        return ObjectUtil.map(deptBO, d -> DeptDomain.builder()
                .number(d.getNumber())
                .id(d.getId())
                .name(d.getName())
                .description(d.getDescription())
                .leader(ObjectUtil.map(d.getLeader(), l -> DeptDomain.Leader.builder()
                        .id(l.getId())
                        .username(l.getUsername())
                        .build()))
                .phone(d.getPhone())
                .email(d.getEmail())
                .status(d.getStatus())
                .sorted(d.getSorted())
                .parentId(d.getParentId())
                .createTime(d.getCreateTime())
                .updateTime(d.getUpdateTime())
                .createBy(d.getCreateBy())
                .updateBy(d.getUpdateBy())
                .build());
    }

    /**
     * 将E回转陈T
     *
     * @param deptDomain 被回转换者
     * @return S 回转换后者
     */
    @Override
    protected DeptBO revert(DeptDomain deptDomain) {
        return ObjectUtil.map(deptDomain, d -> DeptBO.builder()
                .id(d.getId())
                .number(d.getNumber())
                .name(d.getName())
                .description(d.getDescription())
                .leader(ObjectUtil.map(d.getLeader(), l -> DeptBO.Leader.builder()
                        .id(l.getId())
                        .username(l.getUsername())
                        .build()))
                .phone(d.getPhone())
                .email(d.getEmail())
                .status(d.getStatus())
                .sorted(d.getSorted())
                .parentId(d.getParentId())
                .createTime(d.getCreateTime())
                .updateTime(d.getUpdateTime())
                .createBy(d.getCreateBy())
                .updateBy(d.getUpdateBy())
                .build());
    }
}
